package com.wxg.utils;

import java.io.File;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class MyDriver {
	private static Properties mainconf = null;

	private static Properties prop(){
		if(null == mainconf){
			String resPath = "mainconf.properties";
			mainconf = My.prop(resPath);
		}
		return mainconf;
	}
	
	public static WebDriver getFirefoxDriver(){
		
		System.setProperty("webdriver.gecko.driver", prop().getProperty("geckoDriver") );
		File pathBinary = new File( prop().getProperty("firefoxBinary") );
		
		FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);   
		DesiredCapabilities desired = DesiredCapabilities.firefox();
		FirefoxOptions options = new FirefoxOptions();
		desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));
		WebDriver driver = new FirefoxDriver(options);
		
		return driver;
	}
	
	public static WebDriver getChromeDriver(){
		
		System.setProperty("webdriver.chrome.driver", prop().getProperty("chromeDriver") );
		WebDriver driver = new ChromeDriver();
		
		return driver;
	}
	
	/**
	 * Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.SessionNotFoundException
	 * -	* 换成了 [selenium-server](https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server)
	 * -	* 后上面的问题就解决了!
	 * 
	 * @return
	 */
	public static WebDriver getHtmlUnitDriver(){
		WebDriver driver = new HtmlUnitDriver();
		return driver;
	}
	
	/**
	 * 
	 * Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies
	 * 
	 * @return
	 */
	public static WebDriver getPhantomJSDriver(){
		System.setProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
				prop().getProperty("phantomjsBinary") );
		WebDriver driver = new PhantomJSDriver();
		return driver;
	}
	
	
}
